package com.ruoyi.api.client.service.impl;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.ruoyi.api.client.domain.dto.WxUserInfo;
import com.ruoyi.api.client.domain.vo.UmsLoginUserVo;
import com.ruoyi.api.client.domain.vo.UmsUserVo;
import com.ruoyi.api.client.service.IClientAuthService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.HrUnioinIdUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.satoken.StpClientUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.common.utils.weixin.WxMaUtils;
import com.ruoyi.ums.domain.UmsUser;
import com.ruoyi.ums.service.IUmsUserService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zouhuu
 * @description 客户端认证授权 服务层
 * @date 2022/08/01 20:46:13
 */
@Service
public class ClientAuthServiceImpl implements IClientAuthService {

    @Autowired
    private IUmsUserService umsUserService;

    /**
     * 账号密码登录
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @Override
    public R login(String username, String password) {
        UmsUser umsUser = umsUserService.selectUmsUserByUsername(username);

        if (StringUtils.isNull(umsUser)) {
            throw new ServiceException("登录用户：" + username + " 不存在");
        } else if (UserStatus.DELETED.getCode().equals(umsUser.getDelFlag())) {
            throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
        } else if (UserStatus.DISABLE.getCode().equals(umsUser.getStatus())) {
            throw new ServiceException("对不起，您的账号：" + username + " 已停用");
        }

        if (!umsUser.getPassword().equals(password)) {
            throw new ServiceException("对不起，您的用户名密码错误");
        }
        UmsLoginUserVo loginUser = new UmsLoginUserVo();
        UmsUserVo umsUserVo = new UmsUserVo();
        StpClientUtil.login(umsUser.getId());
        loginUser.setUid(umsUser.getId());
        BeanUtils.copyBeanProp(umsUserVo, umsUser);
        loginUser.setUserInfo(umsUserVo);
        loginUser.setToken(StpClientUtil.getTokenValue());
        return R.ok(loginUser);
    }

    /**
     * 微信小程序登录
     */
    @Override
    public R wxmLogin(String appId, String jscode, WxUserInfo wxUserInfo) {
        WxMaJscode2SessionResult jscode2session = null;
        try {
            jscode2session = WxMaUtils.getMaService(appId).jsCode2SessionInfo(jscode);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        String openid = jscode2session.getOpenid();
        String unionid = jscode2session.getUnionid();
        UmsUser user = umsUserService.selectUmsUserByUnionid(unionid);
        if (StringUtils.isNotNull(user)) {
            user.setWxmClientOpenid(openid);
            user.setAvatar(wxUserInfo.getAvatarUrl());
            user.setNickname(wxUserInfo.getNickName());
            umsUserService.updateUmsUser(user);
        } else {
            user = new UmsUser();
            user.setWxmClientOpenid(openid);
            user.setAvatar(wxUserInfo.getAvatarUrl());
            user.setNickname(wxUserInfo.getNickName());
            user.setWxUnionid(unionid);
            umsUserService.insertUmsUser(user);
        }

        UmsUserVo umsUserVo = new UmsUserVo();
        BeanUtils.copyBeanProp(umsUserVo, user);
        UmsLoginUserVo loginUser = new UmsLoginUserVo();
        StpClientUtil.login(user.getId());
        loginUser.setUid(user.getId());
        loginUser.setUserInfo(umsUserVo);
        loginUser.setToken(StpClientUtil.getTokenValue());
        return R.ok(loginUser);
    }

    /**
     * HR Token登录  此方法为内部联合登录方法 不需要使用
     * @param hrtoken
     * @return
     */
    @Override
    public R hrLogin(String hrtoken) {

        HrUnioinIdUtils hrUnioinIdUtils = new HrUnioinIdUtils();

        String unionid = hrUnioinIdUtils.getUnionIdByHrTk(hrtoken);

        UmsUser user = umsUserService.selectUmsUserByUnionid(unionid);

        if (StringUtils.isNull(user)) {
            user = new UmsUser();
            user.setAvatar("");
            user.setNickname("HR" + IdUtils.fastSimpleUUID().substring(0, 8));
            user.setWxUnionid(unionid);

            umsUserService.insertUmsUser(user);
        }
        UmsUserVo umsUserVo = new UmsUserVo();
        BeanUtils.copyBeanProp(umsUserVo, user);
        UmsLoginUserVo loginUser = new UmsLoginUserVo();
        StpClientUtil.login(user.getId());
        loginUser.setUid(user.getId());
        loginUser.setUserInfo(umsUserVo);
        loginUser.setToken(StpClientUtil.getTokenValue());
        return R.ok(loginUser);
    }


}
